package cn.xuchunh.guava.collection;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import java.util.HashSet;
import java.util.Set;

/**
 * Created on 2017/4/3.
 *
 * <p><a href="http://wiki.jikexueyuan.com/project/google-guava-official-tutorial/immutable-collections.html">不可变集合</a></p>
 *
 * @author XCH
 */
public class ImmutableCollection {

    public static void main(String[] args) {
        // 创建
        ImmutableSet<String> colorNames_of = ImmutableSet.of("red",
                "orange",
                "yellow",
                "green",
                "blue",
                "purple");

        Set<String> colorNames = new HashSet<>();
        colorNames.add("red");
        colorNames.add("orange");
        colorNames.add("yellow");
        colorNames.add("green");
        colorNames.add("blue");
        colorNames.add("purple");
        ImmutableSet<String> colorNames_copyOf = ImmutableSet.copyOf(colorNames);

        ImmutableSet<String> colorNames_builder = ImmutableSet.<String>builder()
                .addAll(colorNames)
                .add("pink")
                .build();


        // asList. 所有不可变集合都有一个 asList()方法提供 ImmutableList 视图，来帮助你用列表形式方便地读取集合元素
        ImmutableList<String> colorNamesList = colorNames_of.asList();
        System.out.println(colorNamesList);
    }

}
